package org.vacoor.xqq.hotkey.impl.win;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
import org.vacoor.xqq.hotkey.AbstractOSHotKeyBinder;
import org.vacoor.xqq.hotkey.OSHotKeyConflictException;
import org.vacoor.xqq.hotkey.OSHotKeyEvent;
import org.vacoor.xqq.hotkey.OSHotKeyListener;

/**
 * User: Vacoor
 */
class JIntellitypeKeyBinder extends AbstractOSHotKeyBinder implements HotkeyListener {
    private JIntellitype jIntellitype;
    private boolean conflict;

    public JIntellitypeKeyBinder(JIntellitype jIntellitype, int identifier) {
        super(identifier);
        this.jIntellitype = jIntellitype;
    }

    /**
     * 我去, JIntellitype 当热键冲突也不会抛出异常
     *
     * @param midifier
     * @param key
     * @throws org.vacoor.xqq.hotkey.OSHotKeyConflictException
     *
     */
    @Override
    public void setHotKey(int midifier, int key) throws OSHotKeyConflictException {
        super.setHotKey(midifier, key);
        jIntellitype.removeHotKeyListener(this);
        jIntellitype.registerSwingHotKey(getIdentifier(), midifier, key);
        jIntellitype.addHotKeyListener(this);
    }

    @Override
    public boolean isConflict() {
        return conflict;
    }

    /**
     * JIntellitype 回调
     *
     * @param i
     */
    @Override
    public void onHotKey(int i) {
        if (getIdentifier() == i) {
            for (OSHotKeyListener l : getOSHotKeyListeners()) {
                l.onOSHotKey(new OSHotKeyEvent(getIdentifier(), getMidifier(), getKey()));
            }
        }
    }
}
